package com.example.pei.ithinklearnt.model;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

import com.example.pei.ithinklearnt.AppApplication;
import com.example.pei.ithinklearnt.bean.AlumniCircle;
import com.example.pei.ithinklearnt.bean.Comment;
import com.example.pei.ithinklearnt.bean.shielding;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobBatch;
import cn.bmob.v3.BmobObject;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BatchResult;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.DeleteBatchListener;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListListener;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;


public class AlumniCircleModelImp implements AlumniCircleModel {

    @Override
    public void findAlumniCircleByObjectId(String objectId, final findAlumniCircle listener) {
        BmobQuery<AlumniCircle> query = new BmobQuery<AlumniCircle>();
        query.include("sender");  //同时查找出发送者的信息
        query.getObject(objectId, new QueryListener<AlumniCircle>() {
            @Override
            public void done(AlumniCircle object, BmobException e) {
                if(e==null){
                    listener.success(object);
                }else{
                    listener.failed(e);
                }
            }
        });
    }

    @Override
    public void findAlumniCircleByCommentId(final String objectId, final findAlumniCircle listener) {
        BmobQuery<Comment> query = new BmobQuery<>();
        query.include("dynamic");  //查找出动态的信息
        query.getObject(objectId, new QueryListener<Comment>() {
            @Override
            public void done(Comment object, BmobException e) {
                if(e==null){
                    findAlumniCircleByObjectId(object.getDynamic().getObjectId(),listener);
                }else{
                    listener.failed(e);
                }
            }
        });
    }

    // TODO: 2018/1/17 删除一个动态后，没有消失 如果再点击删除，会删除不掉
    @Override
    public void deleteAlumniCircle(AlumniCircle al, List photoUrls,final deleteAlumniCircle listener) {
        AlumniCircle ac=new AlumniCircle();
        ac.setObjectId(al.getObjectId());
        ac.delete(new UpdateListener(){
            @Override
            public void done(BmobException e) {
                if(e==null){
                    listener.succeed();
                }else{
                    listener.failed();
                }
            }
        });
        //删除服务器中的图片
        if(photoUrls!=null){
            String[] urls =new String[photoUrls.size()];
            for(int i=0;i<urls.length;i++){
                urls[i]=photoUrls.get(i).toString();
            }
            BmobFile.deleteBatch(urls, new DeleteBatchListener() {
                @Override public void done(String[] failUrls, BmobException e) {}
            });
        }
        //删除相关的评论
        BmobQuery<Comment> query = new BmobQuery<>();
        query.addWhereEqualTo("dynamic",al);
        query.findObjects(new FindListener<Comment>() {
            @Override
            public void done(List<Comment> object, BmobException e) {
                if(e==null){
                    if(object!=null&&object.size()!=0){
                        List<BmobObject> comments = new ArrayList<>();
                        comments.addAll(object);
                        new BmobBatch().deleteBatch(comments).doBatch(new QueryListListener<BatchResult>() {
                            @Override public void done(List<BatchResult> o, BmobException e) {

                            }
                        });
                    }
                }
            }
        });
    }

    @Override
    public void shieldAlumniCircle(final String userName, final String objectId,final shieldAlumniCircle listener) {
        BmobQuery<shielding> query = new BmobQuery<shielding>();
        query.addWhereEqualTo("userID", userName);
        //执行查询方法
        query.findObjects(new FindListener<shielding>() {
            @Override
            public void done(final List<shielding> object, BmobException e) {
                if (e == null) {
                    final SharedPreferences.Editor editor= AppApplication.getContext().getSharedPreferences(userName+"_shieldings", Context.MODE_PRIVATE).edit();
                    if(object.size()==1){
                        //服务器存在这条，则修改，增加屏蔽数据
                        final shielding sd=new shielding();
                        sd.setObjectIDs(object.get(0).getObjectIDs());
                        sd.addObjectID(objectId);
                        sd.update(object.get(0).getObjectId(),new UpdateListener() {
                            @Override
                            public void done(BmobException e) {
                                if(e==null){
                                    editor.putInt("size",1+object.get(0).getObjectIDs().size());
                                    editor.putString("num_"+(sd.getObjectIDs().size()-1),objectId);
                                    editor.apply();
                                    listener.succeed();
                                }else {
                                    listener.failed();
                                }
                            }
                        });
                    }else if(object.size()==0){
                        //服务器没有找个人的屏蔽数据，则应该增加
                        final shielding sd=new shielding();
                        sd.setUserID(userName);
                        sd.addObjectID(objectId);
                        sd.save(new SaveListener<String>() {
                            @Override
                            public void done(String s, BmobException e) {
                                if(e==null){
                                    editor.putInt("size",1);
                                    editor.putString("num_0",objectId);
                                    editor.apply();
                                    listener.succeed();
                                }else {
                                    listener.failed();
                                }
                            }
                        });
                    }
                }else {
                    listener.failed();
                }
            }
        });
    }

    @Override
    public void addOneLike(String objectId) {
        AlumniCircle alumniCircle=new AlumniCircle();
        alumniCircle.setObjectId(objectId);
        alumniCircle.increment("praiseNumber");
        alumniCircle.update(new UpdateListener() {
            @Override
            public void done(BmobException e) {
            }
        });
    }

    @Override
    public void addOneComment(String objectId, final addOneComment listener) {
        AlumniCircle alumniCircle=new AlumniCircle();
        alumniCircle.setObjectId(objectId);
        alumniCircle.increment("commentNumber");
        alumniCircle.update(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if(e==null){
                    listener.succeed();
                }else{
                    listener.failed();
                }
            }
        });
    }

    @Override
    public void deleteComment(String commentObjectId,final deleteOneComment listener) {
        AlumniCircle alumniCircle=new AlumniCircle();
        alumniCircle.setObjectId(commentObjectId);
        alumniCircle.increment("commentNumber",-1);
        alumniCircle.update(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if(e==null){
                    listener.succeed();
                }else{
                    listener.failed();
                }
            }
        });
    }

    @Override
    public boolean isDynamicShielded(String objectId, String userName) {
        SharedPreferences preferences=AppApplication.getContext().getSharedPreferences(userName+"_shieldings", Context.MODE_PRIVATE);
        for(int i=0;i<preferences.getInt("size",0);i++){
            if(objectId.equals(preferences.getString("num_"+i,""))){
                return true;
            }
        }
        return  false;
    }

    public interface findAlumniCircle{
        void success(AlumniCircle object);
        void failed(BmobException e);
    }
    public interface deleteAlumniCircle{
        void succeed();
        void failed();
    }
    public interface shieldAlumniCircle{
        void succeed();
        void failed();
    }
    public interface addOneComment{
        void succeed();
        void failed();
    }
    public interface deleteOneComment{
        void succeed();
        void failed();
    }

}
